#! /usr/bin/python
# encoding=utf-8
# Created by Fenglu Niu on 2025/5/22 15:12
from copy import deepcopy

import requests
from cachetools import TTLCache, cached


class Crawler(object):
    __cache = TTLCache(maxsize=10, ttl=60 * 60 * 11)
    __instance = None
    __is_first = True

    def __new__(cls):
        if not cls.__instance:
            cls.__instance = object.__new__(cls)
        return cls.__instance

    headers = {
        'Content-Type': 'application/json',
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36'
    }
    def __init__(self):
        if Crawler.__is_first:
            Crawler.__is_first = False
            self.__base_url = 'https://zfw-dyh.by.gov.cn'
            self.__case_list_url = f'{self.__base_url}/gzdyh-mediate/api/web/caseInfo/pageQueryAll?page={{}}&size=10&sortColmn=1&sortType=2&createStart=2025-01-01&createEnd=2025-12-31&queArea=440118'
            self.__login_url = f'{self.__base_url}/gzdyh-cust/api/web/ctAccount/login'
            self.__switch_url = f'{self.__base_url}/gzdyh-cust/api/web/ctAccount/switchRole?roleCode=22_00024-3'
            self.__case_info_url = f'{self.__base_url}/gzdyh-mediate/api/web/caseInfo/getCaseInfo?id={{}}'
            self.__case_result_url = f'{self.__base_url}/gzdyh-mediate/api/web/caseInfoUnfold/getTransactResult?caseId={{}}'
            self.__case_feedback_url = f'{self.__base_url}/gzdyh-mediate/api/web/caseFeedback/listFeedback?id={{}}'
            self.__case_flow_url = f'{self.__base_url}/gzdyh-mediate/api/web/caseTask/listCaseFlow?caseId={{}}'
            self.__case_apply_record_url = f'{self.__base_url}/gzdyh-mediate/api/web/caseTask/listMyApplyRecord?caseId={{}}'



    @cached(__cache)
    def get_token(self):
        body = {'acc': 'zcqzzzx', 'cipher': 'Gzs@2024'}
        header = deepcopy(self.headers)
        header.setdefault('Referer', f'{self.__base_url}/index.html')
        header.setdefault('Origin', self.__base_url)
        resp = requests.post(self.__login_url, json=body, headers=header)
        token = resp.json()['data']['token']
        header.setdefault('Authorization', f'{token}')
        resp = requests.get(self.__switch_url, headers=header)
        print(resp.json())
        return resp.json()['data']

    def get_case_list(self, page):
        token = self.get_token()
        header = deepcopy(self.headers)
        header.setdefault('Authorization', f'{token}')
        resp = requests.get(self.__case_list_url.format(page), headers=header)
        return resp.json()

    def get_case_info(self, id):
        token = self.get_token()
        header = deepcopy(self.headers)
        header.setdefault('Authorization', f'{token}')
        resp = requests.get(self.__case_info_url.format(id), headers=header)
        print(resp.json())
        return resp.json()['data']

    def get_case_result(self, id):
        token = self.get_token()
        header = deepcopy(self.headers)
        header.setdefault('Authorization', f'{token}')
        resp = requests.get(self.__case_result_url.format(id), headers=header)
        return resp.json()['data']

    def get_case_feedback_list(self, id):
        token = self.get_token()
        header = deepcopy(self.headers)
        header.setdefault('Authorization', f'{token}')
        resp = requests.get(self.__case_feedback_url.format(id), headers=header)
        return resp.json()['data']['caseFeedbackList']

    def get_case_flow_list(self, id):
        token = self.get_token()
        header = deepcopy(self.headers)
        header.setdefault('Authorization', f'{token}')
        resp = requests.get(self.__case_flow_url.format(id), headers=header)
        return resp.json()['data']

    def get_case_apply_record(self, id):
        token = self.get_token()
        header = deepcopy(self.headers)
        header.setdefault('Authorization', f'{token}')
        resp = requests.get(self.__case_apply_record_url.format(id), headers=header)
        return resp.json()['data']